package com.ymatou.iapi.productquery.testcase;

import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymatou.iapi.productmanager.parameter.product.CatalogSaveModel;
import com.ymatou.iapi.productquery.parameter.GetProductIdByPidBean;
import com.ymatou.iapi.productquery.service.GetProductIdByPidCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.ProductManagerCallServiceV2;
import com.ymttest.database.mongo.jproduct.ProductsWapper;
import com.ymttest.utils.EnvSetup;
/**
*获取商品编号
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetProductIdByPid{
	private static GetProductIdByPidBean getproductidbypidBean;
	private static GetProductIdByPidCall getproductidbypidCall;
	private static int sellerId = Integer.parseInt(EnvSetup.getData("SellerId2"));
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("换取商品编号");
	}

	@Before
	public void caseUp() {
		getproductidbypidBean=new GetProductIdByPidBean();
		getproductidbypidCall=new GetProductIdByPidCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getproductidbypidCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetProductIdByPid_001() {
		Logger.start(true,"换取商品编号成功");
		try {
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			// 场景描述1规格新客价格最便宜
			addpro.getProduct().getCatalogList().remove(1);
			CatalogSaveModel c = addpro.getProduct().getCatalogList().get(0);
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Map<String, Object> productMap = new ProductsWapper().getByProductId(productId);
			Integer pid=(Integer) productMap.get("ipid");
			getproductidbypidBean.setPid(pid);
			getproductidbypidCall.setData(getproductidbypidBean);
			getproductidbypidCall.callService();			
			Logger.verifyEquals("200", getproductidbypidCall.getString("Code"), "返回码检查");
			Logger.verifyEquals(productId,getproductidbypidCall.getString("Data"),"验证productid");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetProductIdByPid_002() {
		Logger.start(true,"不输入pid失败");
		try {
			getproductidbypidCall.setData(getproductidbypidBean);
			getproductidbypidCall.callService();			
			Logger.verifyEquals("201", getproductidbypidCall.getString("Code"), "返回码检查");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetProductIdByPid_003() {
		Logger.start(true,"输入pid不存在失败");
		try {
			getproductidbypidBean.setPid(-1);
			getproductidbypidCall.setData(getproductidbypidBean);
			getproductidbypidCall.callService();			
			Logger.verifyEquals("201", getproductidbypidCall.getString("Code"), "返回码检查");
			Logger.verifyIsNull(getproductidbypidCall.getString("Data"),"验证productid");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}